package com.example.test2.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.example.test2.R;
import com.example.test2.databinding.ActivityAddFriendBinding;
import com.example.test2.db.control.UserDbController;
import com.example.test2.db.entity.User;
import com.example.test2.event.FriendEvent;
import com.example.test2.http.Constants;
import com.example.test2.utils.FileUtils;
import com.example.test2.utils.ToastUtils;
import com.example.test2.utils.Tools;

import org.greenrobot.eventbus.EventBus;

import java.util.Date;

public class AddFriendActivity extends BaseActivity<ActivityAddFriendBinding> implements View.OnClickListener{
    private UserDbController dbUserController;
    private User user;

    @Override
    protected void initData() {
        super.initData();
        getBinding().titleBar.setTitle("发布动态");
        dbUserController = UserDbController.getInstance(this);
        user = dbUserController.searchByName(Constants.PHONE);
    }

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().titleBar.setLeftIconOnClickListener(view -> {finish();});
        getBinding().btSend.setOnClickListener(this);
        getBinding().ivImg.setOnClickListener(this);
    }
    private Uri uri;

    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1000 && resultCode == Activity.RESULT_OK && null != data) {
            uri = data.getData();
            getBinding().ivImg.setImageURI(uri);
        }
    }

    private void selectImage() {
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
        intent.setType("image/*");
        startActivityForResult(intent, 1000);
    }

    @Override
    protected int initLayout() {
        return R.layout.activity_add_friend;
    }

    @Override
    protected ActivityAddFriendBinding onCreateViewBinding(@NonNull LayoutInflater layoutInflater) {
        return ActivityAddFriendBinding.inflate(layoutInflater);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.bt_send:
                send();
                break;
            case R.id.iv_img:
                selectImage();
                break;
        }
    }

    String path;
    private void send() {
        String msg = getBinding().etMsg.getText().toString();
        if (uri != null) {
            path = FileUtils.getPath(this, uri, System.currentTimeMillis() + "");
        } else {
            path = "";
        }
        user.setMsg(msg);
        user.setDate(Tools.customFormat(new Date(), "yyyy-MM-dd"));
        user.setPath(path);
        dbUserController.update(user);
        ToastUtils.getInstance().show(AddFriendActivity.this, "发布成功");
        EventBus.getDefault().post(new FriendEvent());
        new Handler().postDelayed(() -> {
            finish();
        }, 500);
    }
}

